﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;
using Lucene.Net.Index;

using CloudSoft.Workflows;

namespace ERPStore.Indexers.Lucene.Workflows.Activities
{

	public sealed class CreateFullTextReport : CodeActivity
	{
		public InArgument<IndexWriter> IndexWriter { get; set; }
		public InArgument<string> IndexPath { get; set; }

		protected override void Execute(CodeActivityContext context)
		{
			var indexWriter = context.GetValue(this.IndexWriter);
			var indexPath = context.GetValue(this.IndexPath);

			int count = indexWriter.MaxDoc();
			indexWriter.Optimize();
			indexWriter.Close();

			string fileName = System.IO.Path.Combine(indexPath, @"catalogindexation.txt");

			var fileInfo = new System.IO.FileInfo(fileName);
			if (fileInfo.Exists)
			{
				System.IO.File.Delete(fileName);
			}

			using (var fs = System.IO.File.CreateText(fileName))
			{
				fs.WriteLine(string.Format("{0} documents created", count));
				fs.Close();
			}

			context.ReportProgress(string.Format("Index builded with {0} documents created", count));
		}
	}
}
